package com.hank.activemq.springboot.VirtualTopic;

import org.apache.activemq.command.ActiveMQMessage;
import org.apache.activemq.command.ConnectionInfo;
import org.apache.activemq.command.RemoveInfo;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.stereotype.Component;

import javax.jms.ConnectionFactory;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class Consumer {

//    @JmsListener(destination = "hank")
    @JmsListener(destination = "hank", containerFactory="jmsListenerContainerQueue")
    public void receiveQueue(String text) {
        System.out.println("收到消息:Receive Hank Queue Message >" + text);
    }

    @JmsListener(destination = "Consumer.A.VirtualTopic.TEST", containerFactory="jmsListenerContainerQueue")
    public void receiveVirtualTopicQueue(String text) {
        System.out.println("收到VirtualTopic消息: >" + text);
    }

    @JmsListener(destination = "Consumer.B.VirtualTopic.TEST", containerFactory="jmsListenerContainerQueue")
    public void receiveVirtualTopicQueue2(String text) {
        System.out.println("收到VirtualTopic2消息: >" + text);
    }

    @JmsListener(destination = "ActiveMQ.Advisory.Connection", containerFactory="jmsListenerContainerTopic")
    public void receiveAdvisoryTopic(ActiveMQMessage message) {

        if (message.getDataStructure() instanceof ConnectionInfo){
            ConnectionInfo info=  (ConnectionInfo)message.getDataStructure();
            System.out.println(getTime()+ "设备上线消息: ClientID>" +info.getClientId()+">>"+info.getConnectionId()+">>"+ info.toString());
        }else if (message.getDataStructure() instanceof RemoveInfo){
            RemoveInfo info=  (RemoveInfo)message.getDataStructure();
            System.out.println(getTime()+"设备下线消息: ClientID>" +info.getObjectId()+">>"+  info.toString());
        }


    }

    public static String getTime(){
        DateFormat df =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(new Date(System.currentTimeMillis()));
    }

    @Bean
    public JmsListenerContainerFactory<?> jmsListenerContainerTopic(@Qualifier("jmsConnectionFactory") ConnectionFactory activeMQConnectionFactory) {
        DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
        bean.setPubSubDomain(true);
        bean.setConnectionFactory(activeMQConnectionFactory);
        return bean;
    }

    @Bean
    public JmsListenerContainerFactory<?> jmsListenerContainerQueue(@Qualifier("jmsConnectionFactory") ConnectionFactory activeMQConnectionFactory) {
        DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
        bean.setPubSubDomain(false);
        bean.setConnectionFactory(activeMQConnectionFactory);
        return bean;
    }
}
